package com.potevio.simulator.strategy;

import org.apache.commons.lang3.StringUtils;

import java.util.Map;

/**
 * @ClassName CommandContext
 * @Description: 策略模式上下文
 * @Date 2021/6/1  10:59
 * @Author zyd
 */
public class CommandContext {

    /**
     * 获取字段类型对应实例
     * @param commandType
     * @return
     */
    public MsgFieldTypeStrategy getTypeInstance(String commandType) {
        try {
            MsgFieldTypeStrategy msgFieldTypeStrategy = null;
            Map<String, String> allClazz = CommandEnum.map;
            String clazz = allClazz.get(commandType.trim());
            if (StringUtils.isNoneEmpty(clazz)) {
                //调用无参构造器创建实例
                msgFieldTypeStrategy = (MsgFieldTypeStrategy) Class.forName(clazz).newInstance();
            }
            return msgFieldTypeStrategy;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取接收报文对应实例
     * @param revertCommand
     * @return
     */
    public ReceiveMsgStrategy getReceiveInstance(String revertCommand){
        try{
            ReceiveMsgStrategy receiveMsgStrategy = null;
            Map<String, String> allClazz = CommandEnum.map;
            String clazz = allClazz.get(("Receive" + revertCommand.trim()).toLowerCase());
            if (StringUtils.isNoneEmpty(clazz)) {
                //调用无参构造器创建实例
                receiveMsgStrategy = (ReceiveMsgStrategy) Class.forName(clazz).newInstance();
            }
            return receiveMsgStrategy;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
